function initSlide() {
    var slide = $api.byId('slide');
    var pointer = $api.domAll('#pointer a');
    window.mySlide = Swipe(slide, {
        // startSlide: 2,
        auto: 5000,
        continuous: true,
        disableScroll: true,
        stopPropagation: true,
        callback: function (index, element) {
            var actPoint = $api.dom('#pointer a.active');
            $api.removeCls(actPoint, 'active');
            $api.addCls(pointer[index], 'active');
        },
        transitionEnd: function (index, element) {

        }
    });
}


function openDiscovery() {
	api.openWin({
        name: 'discovery',
        url: 'discovery/discovery.html'
    });
}

// open group detail
function openGroupDetail(did) {
    api.openWin({
        name: 'actDetail',
        url: 'group/actDetail.html',
        pageParam: {groupid: did}
    });
}

// open my groups page
function openMyGroups() {
	api.openWin({
        name: 'my-groups',
        url: 'my-groups.html',
    });
}

// open huodong page
function openHuodong() {
	api.openWin({
	    name: 'huodong',
	    url: 'huodong.html'
    });
}

function openLoginPage() {
	api.openWin({
	    name: 'login',
	    url: 'userLogin.html'
    });
}

function openPersonPage() {
	api.openWin({
		name: 'person',
		url: 'person.html'
	});
}

function openLifeDetail(title,type){
    api.openWin({
        name: 'life-list',
        url: 'life-list.html',
        opaque: true,
        vScrollBarEnabled: false,
        pageParam:{title:title,type:type}
    });
}

function getData(id) {
    var getTabBarActivityUrl = '/tabBar?filter=';
    var urlParam = {
        include: ["activity"],
        where: {
            id: id
        }
    };
    ajaxRequest(getTabBarActivityUrl + JSON.stringify(urlParam), 'GET', '', function (ret, err) {
        if (ret) {
            var content = $api.byId('act-content');
            var tpl = $api.byId('act-template').text;
            var tempFn = doT.template(tpl);
            content.innerHTML = tempFn(ret[0].activity);
        } else {
            api.alert({
                msg: err.msg
            });
        }
        api.hideProgress();
    })
}

function getMyGroupsData() {
	
	// 删掉旧数据
	if ($api.domAll('.added-group-item').length == 1) {
		$api.remove($api.domAll('.added-group-item')[0]);
	} 
	if ($api.domAll('.added-group-item').length == 2) {
		$api.remove($api.domAll('.added-group-item')[0]);
		$api.remove($api.domAll('.added-group-item')[1]);
	} 
	
	var data = [];
	var url_get_my_groups = 'user';
	
	ajaxRequest(url_get_my_groups, 'GET', {}, function(ret, err) {
		if (ret) {
			if (ret.groups) {
				data = ret.groups;
			}
			
			// 最多显示2个
			if (ret.groups && data.length > 2) {
				data = data.slice(0, 2);
			}						
								
			var content = $api.byId('my-groups-content');
			var tpl = $api.byId('group-item-template').text;
			var tempFn = doT.template(tpl);
			
			$api.prepend(content, tempFn(data));
	
			// 结束下拉刷新动画
			api.refreshHeaderLoadDone();
		} else {
		
			// TODO
            api.alert({
                msg: err.msg
            });
		}
	});
}

function getNearbyGroupsData() {
	
	var data = [];
	var url_get_nearby_groups_data = 'user';
	
	ajaxRequest(url_get_nearby_groups_data, 'GET', {}, function(ret, err) {
		if (ret) {
			if (ret.groups) {
				data = ret.groups;
			}
					
			var content = $api.byId('run-group-lists');
			var tpl = $api.byId('act-template').text;
			var tempFn = doT.template(tpl);
			
			content.innerHTML = '';
			content.innerHTML = tempFn(data);
	
			// 结束下拉刷新动画
			api.refreshHeaderLoadDone();
		} else {
		
			// TODO
            api.alert({
                msg: err.msg
            });
		}
	});
}

function getMoreNearbyGroups() {
	var data = [];
	var url_get_nearby_groups_data = 'user';
	
	ajaxRequest(url_get_nearby_groups_data, 'GET', {}, function(ret, err) {
		if (ret) {
			if (ret.groups) {
				data = ret.groups;
			}
					
			var content = $api.byId('run-group-lists');
			var tpl = $api.byId('act-template').text;
			var tempFn = doT.template(tpl);
			
//			content.innerHTML = tempFn(data);
			$api.append(content, tempFn(data));
	
			// 结束下拉刷新动画
			api.refreshHeaderLoadDone();
		} else {
		
			// TODO
            api.alert({
                msg: err.msg
            });
		}
	});
}

function getBanner(id) {

    var getTabBarBannerUrl = '/tabBar?filter=';
    var urlParam = {
        include: ["banner"],
        where: {
            id: id
        }
    };
    ajaxRequest(getTabBarBannerUrl + JSON.stringify(urlParam), 'GET', '', function (ret, err) {
        if (ret) {
            var content = $api.byId('banner-content');
            var tpl = $api.byId('banner-template').text;
            var tempFn = doT.template(tpl);
            content.innerHTML = tempFn(ret[0].banner);
            initSlide();
        } else {
            api.alert({
                msg: err.msg
            });
        }
    })
}

apiready = function () {
				                
	// for test
	initSlide();
	
	// 初始化数据
    // getBanner(api.pageParam.tid);
    // getData(api.pageParam.tid);
    getMyGroupsData();
    getNearbyGroupsData();
    
    // pull to refresh
    api.setRefreshHeaderInfo({
    
        visible: true,
        bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: false
    }, function (ret, err) {

		getNearbyGroupsData();       
       	getMyGroupsData();
        // getData(api.pageParam.tid);
       
    });


	// 滚动翻页
    api.addEventListener({
        name: 'scrolltobottom'
    }, function (ret, err) {
        getMoreNearbyGroups();
    });

};